package com.cuz.hot100;

import com.cuz.hot100.datastruck.ListNode;

public class Q24SwapNodesInLinkedListPairwise {

    public static ListNode solution1(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode preHead = new ListNode();
        preHead.next = head;
        ListNode pre = preHead;
        ListNode cur = head;
        ListNode next = cur.next;
        while (next != null) {
            ListNode post = next.next;
            pre.next = next;
            next.next = cur;
            cur.next = post;
            pre = cur;
            cur = post;
            if (cur == null) {
                break;
            }
            next = cur.next;
        }
        return preHead.next;
    }
}
